<?php


namespace frontend\controllers\cyf;

use frontend\controllers\common;
use logic\cyf\GoodsLogic;
use servers\AliPayServer;
use servers\cyf\EsServer;
use yii\web\Controller;
use yii\web\Request;
use const http\Client\Curl\PROXY_HTTP;

class GoodsController extends Controller
{
    /**
     * 商品列表
     * @param Request $request
     * @return array
     */
    public function actionIndex(Request $request)
    {
        try {
            $params = $request->get();
            $goods = GoodsLogic::index($params);
            return common::success(20000,$goods,'操作成功');
        }catch (\Exception $exception){
            return common::fail(10001,'null',$exception->getMessage());
        }
    }

    /**
     * 轮播图
     * @return array
     * @name cyf
     */
    public function actionBanner(Request $request)
    {
        try {
            if($request->method != 'GET'){

                return common::fail('20001',null,'请求方式错误');
            }
            $banner = GoodsLogic::banner();
            if(!$banner) throw new \Exception('操作失败');
            return common::success('0',$banner,'操作成功');
        }catch (\Exception $exception){
            return common::fail('0',null,$exception->getMessage());
        }

    }

    /**
     * 推荐类商品类
     * @name cyf
     */
    public function actionType(Request $request)
    {
        try {
            if($request->method != 'GET'){
                return common::fail('20001',null,'请求方式错误');
            }
            $res = $type = GoodsLogic::type();
            if(!$res) throw new \Exception('操作失败');
            return common::success('0',$res,$res['msg']);
        }catch (\Exception $exception){
            return Common::fail('20001',null,$exception->getMessage());
        }

    }

    /**
     * 推荐类商品详情
     * @param Request $request
     * @return false|string
     * @name cyf
     */
    public static function actionRecommend(Request $request)
    {   //验证请求方式
        try {
            if($request->method != 'GET'){
                return common::fail('20001','null','请求方式错误');
            }
            $params = $request->get();
            //验证参数
            if(empty($params['commend_id']) || !is_numeric($params['commend_id'])){
                return common::fail('20002','null','参数错误');
            }
            $data = GoodsLogic::commendGoods($params);

            if($data['status'] == 0){
                return Common::success('0',$data['data'],$data['msg']);
            }


        }catch (\Exception $exception){
            return Common::success('20004','null',$exception->getMessage());
        }
    }

    /**
     * 商品详情
     * @param Request $request
     * @return false|string
     */
    public function actionGoodsDetail(Request $request)
    {
        try {
            if(!$request->isGet) return common::fail('20005','null','请求方式错误');
            $goods_id = $request->get('goods_id');
            if(!is_numeric($goods_id)|| empty($goods_id)) return common::fail(20006,'null','参数错误');
            $goodsDetail = GoodsLogic::goodsDetail($goods_id);//调用逻辑
            if(!$goodsDetail) throw new \Exception('操作失败');
            return common::success(0,$goodsDetail,'操作成功');
        }catch (\Exception $exception){
            return common::fail(20006,'null',$exception->getMessage());
        }
    }



    public function actionAddcar(Request $request)
    {

    }

}